package cn.jeeweb.web.modules.equipment.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.equipment.entity.EqsMaintenanceCheckTemplate;
import cn.jeeweb.web.modules.equipment.entity.EqsMaintenancePlanChecktemplate;
import cn.jeeweb.web.modules.equipment.service.IEqsMaintenanceCheckTemplateService;
import cn.jeeweb.web.modules.equipment.service.IEqsMaintenancePlanCheckTemplateService;
import cn.xjzn.framework_Core.ModuleActionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/equipment/planchecktemplate")
@Log(title = "eqs_maintenance_plan_checktemplate")
public class PlanCheckTemplateController {

    @Autowired
    IEqsMaintenancePlanCheckTemplateService eqsMaintenancePlanCheckTemplateService;

    @Autowired
    IEqsMaintenanceCheckTemplateService eqsMaintenanceCheckTemplateService;

    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @Log(logType = ModuleActionType.INSERT)
    public Response save(String planId,String[] templateIds) {
        if(StringUtils.isNotBlank(planId) && templateIds != null && templateIds.length > 0){
            EntityWrapper<EqsMaintenancePlanChecktemplate> entityWrapper = new EntityWrapper<>(EqsMaintenancePlanChecktemplate.class);
            entityWrapper.eq("planId",planId);
            eqsMaintenancePlanCheckTemplateService.delete(entityWrapper);
            for(String id : templateIds){
                EqsMaintenancePlanChecktemplate template = new EqsMaintenancePlanChecktemplate();
                template.setPlanId(planId);
                template.setCheckTemplateId(id);
                eqsMaintenancePlanCheckTemplateService.insert(template);
            }
            return Response.ok("保存成功");
        }else{
            return Response.error("参数有误");
        }
    }


    @RequestMapping(value = "batchDelete", method = {RequestMethod.GET,RequestMethod.POST})
    @Log(logType = ModuleActionType.DELETE)
    public Response batchDelete(@RequestParam(value = "templateIds", required = false)String[] templateIds) {
        if(templateIds != null && templateIds.length > 0){
            for(String id : templateIds){
                eqsMaintenancePlanCheckTemplateService.deleteById(id);
            }
            return Response.ok("删除成功");
        }else{
            return Response.error("参数有误");
        }
    }

    @RequestMapping(value = "getPlanChecktemplates", method = {RequestMethod.GET,RequestMethod.POST})
    @Log(logType = ModuleActionType.SELECT)
    public Response getPlanChecktemplates(String planId){
        List<EqsMaintenanceCheckTemplate> eqsMaintenanceCheckTemplates = eqsMaintenanceCheckTemplateService.getCheckTemplateByPlanId(planId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("records",eqsMaintenanceCheckTemplates);
        return Response.ok("",map);
    }

}